import threading
import time
from multiprocessing import Queue, Process
import queue
import thread1, thread2, thread3


def GPSProcess(gps_queue):
    while True:
        gps_queue.put("1")
        time.sleep(1)



def PowerSwitchProcess(power_queue):
    # power_queue.put(False)

    # while True:
    # 通过pwm波，检测电平信号，电平信号对应True/False
    while True:
        a = 1
        # print(a)
        power_queue.put(a)
        time.sleep(3 * 10)
        a = 0
        # print(a)
        power_queue.put(a)
        time.sleep(5)




def WaterPumpSwitchProcess(waterpump_queue):
    while True:
        a = 1
        # print(a)
        waterpump_queue.put(a)
        time.sleep(3 * 10)
        a = 0
        # print(a)
        waterpump_queue.put(a)
        time.sleep(5)


def McLinter(gps_queue, power_queue, waterpump_queue):
    linkIdq = queue.Queue()
    t1 = threading.Thread(target=thread1.getgps, args=(gps_queue, linkIdq))
    t1.start()
    t2 = threading.Thread(target=thread2.getpower, args=(power_queue, linkIdq))
    t2.start()
    t3 = threading.Thread(target=thread3.getwater, args=(waterpump_queue,))
    t3.start()


if __name__ == "__main__":
    gps_queue = Queue()
    power_queue = Queue()
    waterpump_queue = Queue()
    p3 = Process(target=PowerSwitchProcess, args=(power_queue,), daemon=True)
    p4 = Process(target=WaterPumpSwitchProcess, args=(waterpump_queue,), daemon=True)
    p1 = Process(target=GPSProcess, args=(gps_queue,), daemon=True)
    p2 = Process(target=McLinter, args=(gps_queue, power_queue, waterpump_queue), daemon=True)  # create a new Process

    p1.start()
    p2.start()
    p3.start()
    p4.start()
    # print("parent_id:%d,p1_id:%d, p2_id:%d,p3_id:%d,p4_id:%d" % (os.getppid(), p1.pid, p2.pid, p3.pid, p4.pid))
    p1.join()
    p2.join()
    p3.join()
    p4.join()
